/*
 * Copyright (c) 2019 Actions Semiconductor Co., Ltd
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/**
 * @file system app main
 */
#include <mem_manager.h>
#include <msg_manager.h>
#include <fw_version.h>
#include <sys_event.h>
#include "app_switch.h"
#include "app_ui.h"
#include <bt_manager.h>
#include <hotplug_manager.h>
#include <input_manager.h>
#include <thread_timer.h>
#include <stream.h>
#include <property_manager.h>
#include <usb/usb_device.h>
#include <usb/class/usb_msc.h>
#include <soc.h>
#include <ui_manager.h>
#include <sys_wakelock.h>
#include <stub_hal.h>
#ifdef CONFIG_PLAYTTS
#include "tts_manager.h"
#endif

#ifdef CONFIG_TOOL
#include "tool_app.h"
#endif

#include "charger.h"
struct thread_timer charger_light_timer;
u8_t charge_status=0;
u8_t exit_charger_mode=0;
void charger_display_timer(struct thread_timer *ttimer, void *expiry_fn_arg)
{
	if(charge_status==0)
	{
		charge_status=1;
		set_corner_lights(RGB_GREEN);

	}
	else
	{
		charge_status=0;
		set_corner_lights(RGB_OFF);

	}
}
void charge_input_event_proc(struct app_msg *msg)
{
	SYS_LOG_INF("key msg:0x%x",msg->cmd);
	switch (msg->cmd) {
	case MSG_SWITCH_TO_ADFU:
		if(hotplug_manager_get_state(HOTPLUG_USB_DEVICE) == HOTPLUG_IN)
		{
			SYS_LOG_INF("enter adfu!!!\n");
			disable_breath_lights();
			set_corner_lights(RGB_RED);
			BROM_ADFU_LAUNCHER(1);
		}		
		break;
	case MSG_EXIT_CHARGER:
		exit_charger_mode=1;
		break;
	default:
		break;
		}
}

int charger_mode_check(void)
{
	struct app_msg msg = {0};
	int result = 0;
	bool terminaltion = false;

	if (!sys_pm_get_power_5v_status()) {
		return 0;
	}

#ifdef CONFIG_SYS_WAKELOCK
	sys_wake_lock(WAKELOCK_USB_DEVICE);
#endif

	charger_view_init();
	init_key_light(0);
	charger_speech_ctl(1);
	disable_breath_lights();
	if(get_bat_status())
	{
		set_corner_lights(RGB_GREEN);
	}
	else
	{
		set_breath_lights(RGB_GREEN);
	}
	
	//thread_timer_init(&charger_light_timer, charger_display_timer, NULL);
	//thread_timer_start(&charger_light_timer, 500, 500);
	exit_charger_mode=0;
	while (!terminaltion) {
		if (receive_msg(&msg, thread_timer_next_timeout())) {
			SYS_LOG_INF("msg tye:%d\n",msg.type);
			switch (msg.type) {
            case MSG_KEY_INPUT:
				system_input_event_handle(msg.value);
				SYS_LOG_INF("exit_charger_mode:%d\n",exit_charger_mode);
				if(exit_charger_mode)
				{
					terminaltion = true;
					exit_charger_mode=0;
				}
				//terminaltion = true;
				break;
			case MSG_INPUT_EVENT:
				charge_input_event_proc(&msg);
				if(exit_charger_mode)
				{
					terminaltion = true;
					exit_charger_mode=0;
				}
				break;
			case MSG_FULL_POWER:
				//thread_timer_stop(&charger_light_timer);
				disable_breath_lights();
				set_corner_lights(RGB_GREEN);
				#if 0
				set_key_lights(KEY_LIGHT_CORNER_LED1,RGB_GREEN);
				set_key_lights(KEY_LIGHT_CORNER_LED2,RGB_GREEN);
				set_key_lights(KEY_LIGHT_CORNER_LED3,RGB_GREEN);
				#endif
				break;
		#ifdef CONFIG_UI_MANAGER
			case MSG_UI_EVENT:
				ui_message_dispatch(msg.sender, msg.cmd, msg.value);
				break;
		#endif
			case MSG_HOTPLUG_EVENT:
				if (msg.value == HOTPLUG_OUT) {
					switch (msg.cmd) {
						case HOTPLUG_CHARGER:
							terminaltion = true;
							if (thread_timer_is_running(&charger_light_timer))
							{
								thread_timer_stop(&charger_light_timer);
							}		
							sys_pm_poweroff();
							break;
						default :
							break;
					}
				}
				else if(msg.cmd==HOTPLUG_USB_DEVICE)
				{
					charger_speech_ctl(0);
				}
				break;
			default:
				//SYS_LOG_ERR("error type: 0x%x! \n", msg.type);
				continue;
			}
			if (msg.callback != NULL)
				msg.callback(&msg, result, NULL);
		}
		thread_timer_handle_expired();
	}
	disable_breath_lights();
	#if 0
	if (thread_timer_is_running(&charger_light_timer))
	{
		thread_timer_stop(&charger_light_timer);
	}
	#endif
	init_key_light(1);
	charger_view_deinit();

#ifdef CONFIG_SYS_WAKELOCK
	sys_wake_unlock(WAKELOCK_USB_DEVICE);
#endif

	return 0;

}
